import android.content.Context;
import android.os.Looper;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.google.gson.internal.LinkedTreeMap;
import com.tnxst.phexamination.R;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.dto.DiabetesFollowDTO;
import com.tnxst.phexamination.dto.InhabitantDTO;
import com.tnxst.phexamination.dto.SignDTO;
import com.tnxst.phexamination.dto.TbFirstFollowDTO;
import com.tnxst.phexamination.public_health_docking_interface.DiabetesInterface;
import com.tnxst.phexamination.public_health_docking_interface.DoctorInterface;
import com.tnxst.phexamination.public_health_docking_interface.FamilyInterface;
import com.tnxst.phexamination.public_health_docking_interface.FamilySignInterface;
import com.tnxst.phexamination.public_health_docking_interface.HypertensionInterface;
import com.tnxst.phexamination.public_health_docking_interface.InhabitantInterface;
import com.tnxst.phexamination.public_health_docking_interface.SystemInterface;
import com.tnxst.phexamination.public_health_docking_interface.TCMHealthElderlyInterface;
import com.tnxst.phexamination.public_health_docking_interface.TuberculosisInterface;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.WebServiceUtils;
import com.tnxst.phexamination.utils.WebUtils;
import com.tnxst.phexamination.dto.FamilyDTO;
import com.tnxst.phexamination.vo.GetSignVo;
import com.tnxst.phexamination.vo.SignVo;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    @Before
    public void before(){
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.tnxst.phexamination", appContext.getPackageName());
        // 用于子线程与主线程通信的Handler，网络请求成功时会在子线程发送一个消息，然后在主线程上接收
        Looper.prepare();
    }

    @After
    public void after(){
        Looper.loop();//增加部分
    }

    @Test
    public void login() {
            SystemInterface.login("cqtnx_test01", "Cqtnx@test01","123" ,new WebServiceUtils.Response<Map>() {
            @Override
            public void onSuccess(Map o) {
                Map<String, Object> msg = (LinkedTreeMap<String,Object>) o;

            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getOrgInfo() {
        SystemInterface.getOrgInfo("cqtnx_test01", "Cqtnx@test01", new WebServiceUtils.Response<Map>() {
            @Override
            public void onSuccess(Map o) {
                Map<String, Object> msg = (LinkedTreeMap<String,Object>) o;

                Log.d("123456", "onSuccess: " + msg.toString());
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void registerProduct() {
        SystemInterface.registerProduct(HISConfig.TEST_COMPANY_CODE, "家庭医生APP",
                "04", "12345678910", "cqtnx_test01", "Cqtnx@test01", new WebServiceUtils.Response<Map>() {
                    @Override
                    public void onSuccess(Map result) {
                        Log.d("123456", "onSuccess: " + result.toString());
                    }

                    @Override
                    public void onError(String error) {
                        Log.d("123456", "onError: " + error);
                    }
                });
    }

    @Test
    public void QueryInhabitantBasicInfo() {
        InhabitantInterface.queryInhabitantBasicInfo("591F3F24E247E22BEBFD0835021BA47E",2000, 0, new WebServiceUtils.Response<List>() {
            @Override
            public void onSuccess(List result) {
                for (Object o : result) {
                    Map<String,Object> map = (Map) o;
                    InhabitantDTO inhabitantVo = (InhabitantDTO) ReflectUtils.mapToObject(map, InhabitantDTO.class);
                    Log.d("123456", inhabitantVo.toString());
                }
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void QueryFamilyArchivesList() {
        FamilyInterface.queryFamilyArchivesList(null, null, null,
                null, null, null, 10, 1, new WebServiceUtils.Response<List>() {
                    @Override
                    public void onSuccess(List result) {
                        for (Object o : result) {
                            Map<String,Object> map = (Map) o;
                            FamilyDTO familyVo = (FamilyDTO) ReflectUtils.mapToObject(map, FamilyDTO.class);
                            Log.d("123456", familyVo.toString());
                        }
                    }

                    @Override
                    public void onError(String error) {
                        Log.d("123456", "onError: " + error);
                    }
                });
    }

    @Test
    public void queryContractByDoctor(){
        FamilySignInterface.queryContractByDoctor("39577BD3E9574B22B85C16FEBD56878F",null,null, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryMedicalStaffList(){
        DoctorInterface.queryMedicalStaffList("510000004926","", 10, 0, new WebServiceUtils.Response<List>() {
            @Override
            public void onSuccess(List result) {
                for (Object o : result) {
                    Log.d("123456", "onSuccess: " + o);
                }

            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void familyDoctorSign(){
        SignVo signVo = new SignVo();
        signVo.setChannel("APP");
        signVo.setFee("1000.00");
        signVo.setContractServices("51791049850");
        signVo.setStartTime("2020-01-11");
        signVo.setEndTime("2027-01-11");
        signVo.setOtheremark("123456");
        signVo.setSignDate("2020-01-01");
        signVo.setPERSON_ID("CF90179D529F7BC0B1E8063731B22A9F");
        signVo.setTeamID("8575A7F9E45B48C429707A26768F1D54");
        signVo.setTeamEmpId("ce781cdbf88745c988c4b0108be259e8");
        signVo.setTags("残疾人");
        signVo.setContractID("07AF0D751F8E749B8B22218B1EFB3AF3");
        FamilySignInterface.familyDoctorSign(signVo, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getOrgServicePack(){
        FamilySignInterface.getOrgServicePack("", 10, new WebServiceUtils.Response<List>() {
            @Override
            public void onSuccess(List result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void haveNetwork(){
        boolean b = WebUtils.haveNetwork();
        System.out.println(b);
    }

    @Test
    public void dao(){
    }

    @Test
    public void getPackContentByPackId(){
        FamilySignInterface.getPackContentByPackId("11", -1, -1, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void SAVE_OR_UPDATE_PACK_CONTENT(){
//        ServiceDetailVo serviceDetailVo = new ServiceDetailVo();
//        serviceDetailVo.setID("3790");
//        serviceDetailVo.setServicesPackId("51791049849");
//        serviceDetailVo.setFRE_NUM("1");
//        serviceDetailVo.setNAME("建立糖尿病患者专案1");
//        serviceDetailVo.setSN("5.01");
//        FamilySignInterface.saveOrUpdatePackContent(serviceDetailVo, new WebServiceUtils.Response() {
//            @Override
//            public void onSuccess(Object result) {
//                Log.d("123456", "onSuccess: " + result);
//            }
//
//            @Override
//            public void onError(String error) {
//                Log.d("123456", "onError: " + error);
//            }
//        });
    }

    @Test
    public void GET_SIGN(){
        GetSignVo getSignVo = new GetSignVo();
        getSignVo.setStatus(2);
        getSignVo.setTeamId("DE10403F63C3E719F95E2C9ED4913EC5");
        FamilySignInterface.getSign(100, 0, getSignVo, new WebServiceUtils.Response<List>() {
            @Override
            public void onSuccess(List result) {
                List<SignDTO> signDTOS = new ArrayList<>();
                for (Object o : result) {
                    Map data = (Map) o;
                    SignDTO signDTO = ReflectUtils.mapToObject(data, SignDTO.class);
                    signDTOS.add(signDTO);
                }
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }


    @Test
    public void GET_TEAM_INFO(){
        FamilySignInterface.getTeamInfo(1, null, 100, 0, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getFamilyDoctorTags(){
        SystemInterface.getFamilyDoctorTags(new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }


    @Test
    public void queryMedicalStaffByDoctorId(){
        DoctorInterface.queryMedicalStaffByDoctorId("ce781cdbf88745c988c4b0108be259e8", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getTeamMemberInfo(){
        FamilySignInterface.getTeamMemberInfo("8575A7F9E45B48C429707A26768F1D54", null, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getSignFormDetail(){
        FamilySignInterface.getSignFormDetail("BBABC9135377394E969C7F4EAA196519", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void get(){
        FamilySignInterface.getServicePackBySign("07AF0D751F8E749B8B22218B1EFB3AF3", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryMemberInfoByOrg(){
        FamilySignInterface.queryMemberInfoByOrg("0", null, null, 10, 0, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void getPersonalPatientInfo(){
        TuberculosisInterface.getPersonalPatientInfo("AFEF03E3C866CF80BE55E8FEDCB19DC3", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void saveOrUpdateTbFirstFollow(){
        TbFirstFollowDTO tbFirstFollowDTO = new TbFirstFollowDTO();
        tbFirstFollowDTO.setTbBaseID("E90760D4CB6DFF5E2F9772333ECF527E");
        tbFirstFollowDTO.setDoctorID("12da6e9c75d94b10944e550bb0e7723e");
        tbFirstFollowDTO.setOtherJson("{}");
        TuberculosisInterface.saveOrUpdateTbFirstFollow(tbFirstFollowDTO, "AFEF03E3C866CF80BE55E8FEDCB19DC3",
                "517002102", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryNewTbFollowList(){
        TuberculosisInterface.queryNewTbFollowList(1000,0,null, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryNewTbFollow(){
        TuberculosisInterface.queryNewTbFollow(null,"65E2AAFCF5D2445304A33B78757D0E64", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }


    @Test
    public void queryDiabetesFollowList(){
        DiabetesInterface.queryDiabetesFollowList(null,1000,0, new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryPersonalDiabetesFollow(){
        DiabetesInterface.queryPersonalDiabetesFollow("385A0734C233A1E1E8EFA9B93553EEBD",new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryPersonalHypertensionFollowList(){
        HypertensionInterface.queryPersonalHypertensionFollowList(1000,0,"",new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }
//[{DoctorName=测试通用账号, MedicationCompliance=2.0, PsychologicalAdjustmentStr=良好, FuClassification=1.0, ComplianceBehavior=1.0, FollowUpDate=2022-02-21, Dbp=56, PersonID=0199BB5A399071C4D61E52685473C60E, PsychologicalAdjustment=1.0, WayUpStr=门诊, ID=93997FDB150C53295C113870B62F74C8, FollowUpDateStr=2022-02-21, Symptom=1.0, Sbp=123, PersonName=百意义, SaltIntake=2.0, ComplianceBehaviorStr=良好, FuClassificationStr=控制满意, SymptomStr=无症状, Sophistication=1, MedicationComplianceStr=间断, SaltIntakeStr=中, FollowUpRemarks=, WayUp=1}, {DoctorName=测试通用账号, MedicationCompliance=1.0, PsychologicalAdjustmentStr=一般, FuClassification=1.0, ComplianceBehavior=4.0, FollowUpDate=2022-02-18, Dbp=88, PersonID=0199BB5A399071C4D61E52685473C60E, PsychologicalAdjustment=2.0, WayUpStr=门诊, ID=84C6AEFC0D5D44E31D225DD51121FBAB, FollowUpDateStr=2022-02-18, Symptom=131466.0, Sbp=123, PersonName=百意义, SaltIntake=1.0, ComplianceBehaviorStr=差, FuClassificationStr=控制满意, SymptomStr=头痛头晕|眼花耳鸣|四肢发麻|下肢水肿|其他, Sophistication=1, MedicationComplianceStr=规律, SaltIntakeStr=轻, FollowUpRemarks=初步初步血小板, WayUp=1}, {DoctorName=测试通用账号, MedicationCompliance=2.0, PsychologicalAdjustmentStr=一般, FuClassification=4.0, ComplianceBehavior=4.0, FollowUpDate=2022-02-17, Dbp=121, PersonID=89357F927FC637212B871B4A27FA3157, PsychologicalAdjustment=2.0, WayUpStr=电话, ID=5A831EB9590A6A57C950014AD68999B2, FollowUpDateStr=2022-02-17, Symptom=14.0, Sbp=123, PersonName=甘婧洁, SaltIntake=2.0, ComplianceBehaviorStr=差, FuClassificationStr=不良反应, SymptomStr=头痛头晕|恶心呕吐|眼花耳鸣, Sophistication=1, MedicationComplianceStr=间断, SaltIntakeStr=中, WayUp=4}, {DoctorName=测试通用账号, MedicationCompliance=1.0, PsychologicalAdjustmentStr=一般, FuClassification=4.0, ComplianceBehavior=4.0, FollowUpDate=2022-02-17, Dbp=123, PersonID=89357F927FC637212B871B4A27FA3157, PsychologicalAdjustment=2.0, WayUpStr=门诊, ID=D8F4F2A2A4C9C620D5C36B78392E7F84, FollowUpDateStr=2022-02-17, Symptom=20.0, Sbp=123, PersonName=甘婧洁, SaltIntake=2.0, ComplianceBehaviorStr=差, FuClassificationStr=不良反应, SymptomStr=恶心呕吐|呼吸困难, Sophistication=1, MedicationComplianceStr=规律, SaltIntakeStr=中, WayUp=1}, {DoctorName=测试通用账号, MedicationCompliance=2.0, PsychologicalAdjustmentStr=一般, FuClassification=2.0, ComplianceBehavior=2.0, FollowUpDate=2022-02-17, Dbp=111, PersonID=6A61ED82435F7601040E8C4A55A709EC, PsychologicalAdjustment=2.0, WayUpStr=家庭, ID=969D0D19BC62F04B507D4BCAB2732373, FollowUpDateStr=2022-02-17, Symptom=142.0, Sbp=178, PersonName=哥特人, SaltIntake=4.0, ComplianceBehaviorStr=一般, FuClassificationStr=控制不满意, SymptomStr=头痛头晕|恶心呕吐|眼花耳鸣|四肢发麻, Sophistication=1, MedicationComplianceStr=间断, SaltIntakeStr=重, FollowUpRemarks=他个人, WayUp=2}, {DoctorName=测试通用账号, MedicationCompliance=4.0, PsychologicalAdjustmentStr=差, FuClassification=4.0, ComplianceBehavior=1.0, FollowUpDate=2022-02-17, Dbp=84, PersonID=FB45899DA4A982B361334CD74234902F, PsychologicalAdjustment=4.0, WayUpStr=家庭, ID=7F7208F2DEDE4BF52B513F615898FABA, FollowUpDateStr=2022-02-17, Symptom=131582.0, Sbp=144, PersonName=项城, SaltIntake=2.0, ComplianceBehaviorStr=良好, FuClassificationStr=不良反应, SymptomStr=头痛头晕|恶心呕吐|眼花耳鸣|呼吸困难|心悸胸闷|鼻衄|四肢发麻|下肢水肿|其他, Sophistication=1, MedicationComplianceStr=不服药, SaltIntakeStr=中, FollowUpRemarks=貌似我李春雨三十 压下, WayUp=2}, {DoctorName=测试通用账号, MedicationCompliance=4.0, PsychologicalAdjustmentStr=一般, FuClassification=2.0, ComplianceBehavior=1.0, FollowUpDate=2022-02-17, Dbp=66, PersonID=F9A1F971D3166CC4A1B107A9A07617BF, PsychologicalAdjustment=2.0, WayUpStr=家庭, ID=2D8DC3FD102166E00DA28A3AB3200040, FollowUpDateStr=2022-02-17, Symptom=16.0, Sbp=142, PersonName=多少岁
            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    //2D8DC3FD102166E00DA28A3AB3200040
    @Test
    public void queryPersonalHypertensionFollow(){
        HypertensionInterface.queryPersonalHypertensionFollow("2D8DC3FD102166E00DA28A3AB3200040",new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);

            }
            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }

    @Test
    public void queryTcmElderList(){
        TCMHealthElderlyInterface.queryTcmElderList(1000,0,"", new WebServiceUtils.Response<List>() {
            @Override
            public void onSuccess(List result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });


    }
    //ID -> 404CA8F11A6E8EAE1A10A464363EC235
    @Test
    public void queryTcmElderFollowDetails(){
        TCMHealthElderlyInterface.queryTcmElderFollowDetails("404CA8F11A6E8EAE1A10A464363EC235", new WebServiceUtils.Response<Map>() {
            @Override
            public void onSuccess(Map result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }


    @Test
    public void queryTbFirstFollow(){
        TuberculosisInterface.queryTbFirstFollow("E90760D4CB6DFF5E2F9772333ECF527E", new WebServiceUtils.Response() {
            @Override
            public void onSuccess(Object result) {
                Log.d("123456", "onSuccess: " + result);
            }

            @Override
            public void onError(String error) {
                Log.d("123456", "onError: " + error);
            }
        });
    }
}